﻿using System.Collections.Generic;
using DAL.Common;
using DAL.POCO;
using Dapper;
using System.Data.SqlClient;

namespace DAL.Repositories
{
    public class FieldRepository
    {
        public static IEnumerable<Field> GetFields(int roundId)
        {
            string sql = "SELECT * FROM [Field] WHERE roundId = @id";

            var fields = MyConnection.Current.Query<Field>(sql, new { id = roundId });
            return fields;
        }

        public static void InsertFields(int roundId, List<Field> fields)
        {
            SqlTransaction trans = MyConnection.Current.BeginTransaction();
            string sql = "INSERT INTO [Field] (roundId, x, y, type, difficulty, radiusGuard) VALUES (@roundId, @x, @y, @type, @difficulty, @radiusGuard)";

            try
            {
                MyConnection.Current.Execute(sql, fields, transaction: trans);
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
            }
        }

        public static void DeleteFields(int roundId)
        {
            string sql = "DELETE FROM [Field] WHERE roundId = @id";

            MyConnection.Current.Execute(sql, new { id = roundId });
        }
    }
}
